package com.in.mobilerechargedth;

import java.io.File;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;

import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;

import com.in.mobilerechargedth.constant.ConstantVariables;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class BaseActivity extends Activity implements OnClickListener {

    public SharedPreferences mPrefs;
    SharedPreferences.Editor editor;
    AdView adView;
    String AD_UNIT_ID = "a152aebb9343d4e";
    private InterstitialAd interstitialAds = null;
    String currentClass;
    static int flash_flag = 0;
    /*private int postdelay = 4;
    private Handler myHandler = null;*/

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.layout_header);

        /*mPrefs = PreferenceManager
                .getDefaultSharedPreferences(getApplicationContext());
        int old_versioncode = mPrefs.getInt(ConstantVariables.VERSION_CODE, 0);
        int v = 0;
        try {
            v = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
            if (v != old_versioncode) {

                // Delete old database file
                File dbFile = getDatabasePath(ConstantVariables.DATABASE_NAME);
                dbFile.deleteOnExit();

                editor = mPrefs.edit();
                editor.putInt(ConstantVariables.VERSION_CODE, v);
                editor.commit();
            }
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }*/
        currentClass = this.getClass().getSimpleName();
        this.setupInterstitialAds();

    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.sharebtn:

            Intent intentShare = new Intent(Intent.ACTION_SEND);
            intentShare.setType("text/plain");
            intentShare
                    .putExtra(
                            android.content.Intent.EXTRA_TEXT,
                            getString(R.string.app_name)
                                    + " /r/n "
                                    + "https://play.google.com/store/apps/details?id=com.in.dthrecharge");
            startActivity(Intent.createChooser(intentShare, "Share"));
            break;
        case R.id.disclaimerbtn:
            Intent i = new Intent(this,
                    DisclaimerActivity.class);
            startActivity(i);
            break;

        default:
            break;
        }
    }

    protected void setupInterstitialAds() {
        // Create an ad.
        this.interstitialAds = new InterstitialAd(this);
        this.interstitialAds.setAdUnitId(AD_UNIT_ID);// "a150272e162142c"
                                                     // AD_UNIT_ID
        // Set the AdListener.
        this.interstitialAds.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                /*myHandler = new Handler();
                myHandler.postDelayed(runRepeatIntertitialAds,
                        postdelay * 1000 * 60);*/
                Log.e("aaaaaaaaaaaaaaa",
                        "Interstitial ad was ready to be shown. onAdLoaded ");
                if (flash_flag == 0) {
                    Log.e("aaaaaaaaaaaaaaa",
                            "Interstitial ad was ready to be shown. onAdLoaded ");
                    flash_flag = 1;
                    showInterstitial();
                }
            }

            @Override
            public void onAdFailedToLoad(int errorCode) {
                Log.e("aaaaaaaaaaaaaaa",
                        "Interstitial ad was not ready to be shown. onAdFailedToLoad "
                                + errorCode);
                //setupInterstitialAds();
            }
        });
        final TelephonyManager tm = (TelephonyManager) getBaseContext()
                .getSystemService(Context.TELEPHONY_SERVICE);
        String deviceid = tm.getDeviceId();

        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                .addTestDevice(deviceid).build();

        this.interstitialAds.loadAd(adRequest);

    }

    /*private Runnable runRepeatIntertitialAds = new Runnable() {
        public void run() {
            showInterstitial();
            // write here whaterver you want to repeat
            myHandler.postDelayed(this, postdelay * 1000 * 60);
        }
    };*/

    protected void setupAdmob() {
        // Create an ad.
        adView = new AdView(this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId(AD_UNIT_ID);
        LinearLayout layout = (LinearLayout) findViewById(R.id.rootLayout);
        layout.addView(adView);

        final TelephonyManager tm = (TelephonyManager) getBaseContext()
                .getSystemService(Context.TELEPHONY_SERVICE);
        String deviceid = tm.getDeviceId();
        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                .addTestDevice(deviceid).build();

        // AdRequest adRequest = new AdRequest.Builder().build();
        adView.loadAd(adRequest);

    }

    protected void share() {

        ImageView shareImg = (ImageView) findViewById(R.id.sharebtn);
        shareImg.setOnClickListener(this);
        Button disclaimerbtn = (Button) findViewById(R.id.disclaimerbtn);
        disclaimerbtn.setOnClickListener(this);
    }

    /** Called when the Show Interstitial button is clicked. */
    public void showInterstitial() {

        if (this.interstitialAds.isLoaded()) {
            this.interstitialAds.show();
        } else {
            Log.e("", "Interstitial ad was not ready to be shown.");
        }
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        /*if (myHandler != null) {
            myHandler.removeCallbacks(runRepeatIntertitialAds);
        }*/
    }

}